%%--------------------------------------------------------------------
%% Author: <vjache>
%% Created: 30.03.2009
%% Description: Iterator over all keys in an ets table.
%%--------------------------------------------------------------------
-module(vect_mnesia_iter_all).

%%--------------------------------------------------------------------
%% Include files
%%--------------------------------------------------------------------
-include("../include/iter.hrl").
%%--------------------------------------------------------------------
%% Exported Functions
%%--------------------------------------------------------------------
-export([
		 new/2,
		 open/1,
		 next/1,
		 close/1,
		 test/0
		 ]).

%%--------------------------------------------------------------------
%% API Functions
%%--------------------------------------------------------------------

new(Table,PageSize) -> 
	iter:from_module(?MODULE, {Table,PageSize}).

open({Table,PageSize}) ->
%% 	{Patt,ResultPatt}=lists:foldl(fun(Idx,{Acc1,Acc2})->
%% 										  Var=list_to_atom([$$|integer_to_list(Idx)]),
%% 										  {erlang:append_element(Acc1, Var),
%% 										   case Idx of
%% 											   1 ->
%% 												   Acc2;
%% 											   _ ->
%% 												   erlang:append_element(Acc2, Var)
%% 										   end}
%% 								  end,{{},{}},lists:seq(1,mnesia:table_info(Table,arity))),
%% 	MatchSpec=[{Patt,[],[{ResultPatt}]}],
	MatchSpec=[{mnesia:table_info(Table,wild_pattern),[],['$_']}],
	R={Table,vect_mnesia:activity(fun()->
										mnesia:select(Table, MatchSpec, PageSize, read)
								end)},
	R.


next({_Table,'$end_of_table'}=S) ->
	{S,?EOF};
next({Table,{[],Cont}}) ->
	next({Table,vect_mnesia:activity(fun()->
								 mnesia:select(Cont)
						 end)});
next({Table,{[First|Objects],Cont}}) ->
	{{Table,{Objects,Cont}},First}.
close(_S) ->
	
	ok.

%%--------------------------------------------------------------------
%% Test Functions
%%--------------------------------------------------------------------

test() ->
	I=new(ring,1000),
	iter:foreach(I,fun(E)->
						   io:format("~p~n",[E])
				 end),
	ok.

%%--------------------------------------------------------------------
%% Local Functions
%%--------------------------------------------------------------------